package com.yuan.controller;

import com.yuan.bean.Person;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Random;
import java.util.Set;

/*
 *@author 雨安
 *类名： MyController3
 *创建时间：2023/10/23
 */
@Controller
@Slf4j
public class MyController3 {


    @ResponseBody //将影响数据设置为JSON或字符串格式响应到网页
    @RequestMapping("/testDemo1")
        //访问后缀名
        //执行方法
    String test(String imgDemo1, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        //打印输入流
        FileInputStream fileInputStream = new FileInputStream("D:\\SpringBoot2\\SpringBoot2_Demo1\\src\\main\\resources\\b.txt");
        //定义接收参数
        String YanZhengMa = "";
        //定义默认数
        int len;
        //获取内容
        while ((len = fileInputStream.read()) != -1) {
            //获取内容
            System.out.println((char) len);
            //创建验证码
            YanZhengMa = YanZhengMa + (char) len;
        }
        ;
        //返回验证码
        return YanZhengMa;
    }


    @ResponseBody()
    @RequestMapping("/testDemo2")
    String tset(@RequestParam("name") String name) {
        log.info("请求进入了");
        System.out.println("Hello：" + name);
        return "";
    }


    @Autowired //自动装配
    Person person; //装配对象

    @RequestMapping("/testDemo3")
    String imgTest(Model model) throws IOException {
        //定义随机对象
        Random random = new Random();
        //定义随机数
        int i = random.nextInt(10);
        //进行判断，如果 == 10 则 -1
        if (i == 10) {
            //重新赋值
            i = i - 1;
        }
        //定义随机数
        int i1 = random.nextInt(10);
        //进行判断，如果 == 10 则 -1
        if (i1 == 10) {
            //重新赋值
            i1 = i1 - 1;
        }
        //定义随机字母
        int z1 = random.nextInt(24);
        //定义随机字母
        int z2 = random.nextInt(24);
        //定义字符串数组
        String[] strings = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
        //定义字符串数组
        String[] strings2 = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
        //定义创建好的验证码
        String YanZheng = i + strings[z1] + i1 + strings2[z2];
        //创建输出流
        FileOutputStream fileOutputStream = new FileOutputStream("D:\\SpringBoot2\\SpringBoot2_Demo1\\src\\main\\resources\\b.txt");
        //强制类型转换
        byte[] bytes = YanZheng.getBytes(StandardCharsets.UTF_8);
        //写入数据
        fileOutputStream.write(bytes);

        //创建输出流
        FileInputStream fileInputStream = new FileInputStream("D:\\SpringBoot2\\SpringBoot2_Demo1\\src\\main\\resources\\b.txt");
        //定义字符串数组
        String GetYanZheng = "";
        //读取数据 读取第一个对象的ASCII码
        int len;
        //循环打印读取数据 打印强制类型转换
        while ((len = fileInputStream.read()) != -1) {
            //将验证码加入到集合中
            GetYanZheng = GetYanZheng + (char) len;
        }
        //设置传递的验证码
        model.addAttribute("yzm", GetYanZheng);
        System.out.println(GetYanZheng);
        //关闭输入输出流
        fileInputStream.close();
        fileOutputStream.close();
        return "/Enroll.html";
    }

    @RequestMapping("/imgTest")
    String haha() {

        return "";
    }
}

